#ifndef RETRIEVE_PCAP
#define RETRIEVE_PCAP
#endif
#define equal(a,b) ((a->BasicInfo).Ip.Dst==(b->BasicInfo).Ip.Dst)&&((a->BasicInfo).Ip.Src==(b->BasicInfo).Ip.Src)&&((a->BasicInfo).Port.Src==(b->BasicInfo).Port.Src)&&((a->BasicInfo).Port.Dst==(b->BasicInfo).Port.Dst)&&((a->BasicInfo).Protocol==(b->BasicInfo).Protocol)

struct IP
{
    unsigned long Src;
    unsigned long Dst;
};

struct PORT
{
    unsigned short Src;
    unsigned short Dst;
};

struct FiveTuple
{
    struct IP Ip;    
    struct PORT Port;
    unsigned char Protocol;	//protocol in ip header
};

struct packet
{
    unsigned int captureLen;
    unsigned int ipHeaderLen;
    struct FiveTuple BasicInfo;
};

struct flow	// bi-directional flow
{
    unsigned int count;
    struct FiveTuple BasicInfo;
    unsigned int PKG_SIZE[5];
    struct flow *next;		// ptr for constructing flow list
};

// statistic for retrieve result
struct statistic
{
    int SingleTotOfAail;
    int SingleTotOfFlow;
    int TotOfPkg;
    int TotOfAail;
    int TotOfFlow;
    int TotOfSize;
};

struct type 
{
    char ProtocolName[10];
    struct type *next;
};

// skip n bytes in file
void skip(FILE *fp, int n)
{
    int i;
    for(i=0;i<n;i++)
        fgetc(fp);
}

// convert 1 byte in file to num 0~255
int convert(FILE *fp)
{
    char temp=fgetc(fp);
    int num=temp;
    return (num<0?num+256:num);
}

// convert 4bytes in file to uint
void process_4B_uint(FILE *fp, unsigned int *num)
{
    int i, temp;
    *num=0;
    for(i=0; i<4; i++)
    {
        temp=convert(fp);
        (*num)+=(unsigned int)(temp)<<(8*i);
    }
}

//convert 2 bytes in file to ushort
void process_2B_ushort(FILE *fp, unsigned short *num)
{
    int i, temp;
    *num=0;
    for(i=0; i<2; i++)
    {
        temp=convert(fp);
        (*num)+=(unsigned short)(temp)<<(8*i);
    }
}
// process the port area, convert to ushort
void process_port(FILE *fp, unsigned short *port)
{
    unsigned char ch[2];
    unsigned short subPort[2];
    ch[0]=fgetc(fp);
    //    printf("ch[0]=%d,",ch[0]);
    ch[1]=fgetc(fp);
    //   printf("ch[1]=%d\n",ch[1]);
    subPort[0]=(unsigned short)(ch[0])<<8;
    subPort[1]=(unsigned short)(ch[1]);
    *port=subPort[0]|subPort[1];
    //  printf("Port:%d\n",*port);
    // int i;
    //  scanf("%d",&i);
}
//process the ip area, convert to ulong
void process_ip(FILE *fp, unsigned long *ip)
{
    unsigned char ch;
    unsigned long subIP[4];
    int i;
    // printf("IP:");
    for( i=0; i<4; i++)
    {
        ch=fgetc(fp);
        //     printf("%d.",ch);
        subIP[i]=(unsigned long)(ch<<(8*i));
    }
    //  printf("\n");
    //  scanf("%d",&i);
    *ip=subIP[0]|subIP[1]|subIP[2]|subIP[3];
}
